﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SHRIS.MIS.Entity.ScadaData;
using SHRIS.MIS.IService.APIExternal;
using SHRIS.MIS.Model.Param.APIExternal;
using SHRIS.MIS.Service.LinePlantService;
using SHRIS.MIS.Web.Controllers;
using SHRIS.MIS.Web.Filter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SHRIS.MIS.Web.Areas.ToolManage.Controllers
{
    [Area("ToolManage")]
    public class ToolDetailController : BaseController
    {
        #region 服务注入
        private readonly IMapper _mapper;
        private LinePlantService _linePlantService;
        public ToolDetailController(IMapper mapper, LinePlantService linePlantService)
        {
            _mapper = mapper;
            _linePlantService = linePlantService;
        }
        #endregion

        #region 视图功能

        [AuthorizeFilter("tool:preview")]
        public IActionResult CC_01()
        {
            return View();
        }

        [AuthorizeFilter("tool:preview")]
        public IActionResult CC_02()
        {
            return View();
        }

        [AuthorizeFilter("tool:preview")]
        public IActionResult CC_03()
        {
            return View();
        }

        #endregion

        #region API服务
        /// <summary>
        /// 获取刀库和刀具信息
        /// </summary>
        /// <param name="deviceCode"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetToolData(string deviceCode)
        {
            var result = _linePlantService.GetToolByDeviceCode(deviceCode);
            return Json(result);
        }

        [HttpGet]
        public async Task<IActionResult> GetToolDetail(string deviceCode,int toolIndex)
        {
            var result = _linePlantService.GetToolDetail(deviceCode,toolIndex);
            return Json(result);
        }

        #region 使用数据库的老方法
        //[HttpGet]
        //public async Task<IActionResult> GetToolStoreInfo(string deviceCode)
        //{
        //    // 通过deviceCode获得deviceId
        //    var thisDevice = await _toolService.SearchDeviceByDeviceCode(deviceCode);
        //    /**
        //     * 通过分析thisDevice获取DeviceId
        //     */
        //    Guid deviceId = new Guid("d0db6f6a-a34d-47a2-a6bf-787665729a87");
        //    var toolStoreInfo = await _toolService.SearchToolStoreInfo(deviceId);
        //    var obj = new
        //    {
        //        toolStoreInfo = toolStoreInfo
        //    };
        //    return Json(obj);
        //}

        //[HttpGet]
        //public async Task<IActionResult> GetToolInfo(string houseId)
        //{
        //    Guid _houseId = new Guid(houseId);
        //    var toolInfo = await _toolService.SearchToolInfo(_houseId);
        //    var obj = new
        //    {
        //        toolInfo = toolInfo
        //    };
        //    return Json(obj);
        //}

        //[HttpGet]
        //public async Task<IActionResult> GetToolOffset(string toolId)
        //{
        //    Guid _toolId = new Guid(toolId);
        //    var toolOffset = await _toolService.SearchToolOffset(_toolId);
        //    var obj = new
        //    {
        //        toolOffset = toolOffset
        //    };
        //    return Json(obj);
        //}

        //[HttpGet]
        //public async Task<IActionResult> GetToolOffsetHistory(string toolId,DateTime stime,DateTime etime)
        //{
        //    Guid _toolId = new Guid(toolId);
        //    var toolOffset = await _toolService.SearchToolOffset(_toolId, stime, etime);
        //    var obj = new
        //    {
        //        toolOffset = toolOffset
        //    };
        //    return Json(obj);
        //}
        #endregion
        #endregion

        #region 辅助方法
        /// <summary>
        /// 将HTTP请求对象转换为便于操作的JSON对象
        /// </summary>
        /// <param name="data">req对象</param>
        /// <returns>JSON对象</returns>
        public IDictionary<string, object> ChangeJsonObject(object data)
        {
            var jsonobj = JsonConvert.SerializeObject(data);
            var result = JsonConvert.DeserializeObject<IDictionary<string, object>>(jsonobj);
            return result;
        }
        #endregion
    }
}
